package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Message;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.service.MessageService;
import com.adminx.service.ResourceService;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;

@Controller
@RequestMapping("message")
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 初始化页�?
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String init(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/message/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/message/init.html");
		return "admin/message/message";
	}
	
	/**
	 * 分页查询�?��消息
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public String query(ModelMap map,Integer pageIndex, Integer pageSize){
		try {
			PageResults<Message> pageResults = this.messageService.query(pageIndex,pageSize);
			map.put("pageResults", pageResults);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/message/message_data";
	}
	
	/**
	 * 查询消息详细信息
	 * @param accountId
	 * @return Account
	 */
	@RequestMapping(value = "/info") 
	@ResponseBody
	public Map<String, Object> info(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Message message = this.messageService.info(id);
			map.put("message", message);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 保存或新增账�?
	 * @param account
	 * @return 
	 */
	@RequestMapping(value = "/save") 
	@ResponseBody
	public Map<String, Object> save(Message message){
		Map<String, Object> map = new HashMap<String, Object>(); 
		try{
			this.messageService.save(message);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 删除消息
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.GET) 
	public Map<String, Object> delBatch(String ids){
		Map<String, Object> map = new HashMap<String, Object>(); 
		try{
			int delNum = this.messageService.delBatch(ids);
			map.put("delNum", delNum);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	/**
	 * 推�?消息
	 */
	@ResponseBody
	@RequestMapping(value = "/push", method = RequestMethod.GET) 
	public Map<String, Object> push(String accountIds, Long messageId){
		Map<String, Object> map = new HashMap<String, Object>(); 
		try{
			this.messageService.push(accountIds, messageId);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
